import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Type } from 'src/entity/Type';
import { IsNull, Repository } from 'typeorm';

@Injectable()
export class TypeService {
  constructor(
    @InjectRepository(Type)
    private typeRepository: Repository<Type>,
  ) { }

  // 获取类型列表
  async getTypeList() {
    const sqlDataList = await this.typeRepository.find({
      where: {
        isDelete: IsNull(),
      },
    });
    return sqlDataList.map((item) => {
      return {
        label: item.model,
        value: item.id.toString(),
      };
    });
  }

  // 通过类型编码获取类型数据
  async getTypeByCode(typeCode: string) {
    return await this.typeRepository.findOne({
      where: {
        isDelete: IsNull(),
        typeCode,
      },
    });
  }
}
